﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;


namespace WebSite.admin.warehouse
{
    public partial class stockGoodsIn : BaseAdmin
    {
        readonly DALWarehouseGoods theWarehouseGoods = new DALWarehouseGoods();
        readonly DALWarehouse theWarehouse = new DALWarehouse();
        readonly DALModelType theModelType = new DALModelType();
        readonly DALStockOrderGoods theStockOrderGoods = new DALStockOrderGoods();
        readonly DALStockOrder theStockOrder = new DALStockOrder();
        tbStockOrderGoods oStockOrderGoods;
        tbWarehouseGoods oWarehouseGoods;
        tbModelType oModelType;
        readonly DALGoods theGoods = new DALGoods();
        tbGoods oGoods;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(ERule.StockOrderList);
                this.BindModelTypeList();
                this.BindWarehouse();
                this.BuildForm();

            }
        }




        void BindWarehouse()
        {
            DataTable dt = theWarehouse.GetList(" flag=1").Tables[0];
            if (dt != null)
            {
                this.ddlWarehouse.DataTextField = "warehouseName";
                this.ddlWarehouse.DataValueField = "id";
                this.ddlWarehouse.DataSource = dt;
                this.ddlWarehouse.DataBind();
            }
            this.ddlWarehouse.Items.Insert(0, new ExtAspNet.ListItem("请选择仓库", "-1"));


        }
        void BuildForm()
        {

                oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
                if (oGoods != null)
                {
                    this.lblGoodsCode.Text = oGoods.goodsCode;
                    this.lblGoodsName.Label = "商品名称";
                    this.lblGoodsName.Text = oGoods.goodsName;
                }
         
                this.lblGoodsBatchNo.Text = Request.QueryString["code"];

                string sModelTypeId = "0";
                if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
                {
                    sModelTypeId = Request.QueryString["mid"];
                }
            oStockOrderGoods = theStockOrderGoods.GetModel(Request.QueryString["code"], int.Parse(Request.QueryString["gid"]),int.Parse(sModelTypeId));
            if (oStockOrderGoods != null)
            {
                this.txtCount.Text = (oStockOrderGoods.goodsCount - oStockOrderGoods.InWarehouseCount).ToString();
            }
            this.lblCostPrice.Text = Request.QueryString["price"];
        }
        void BindModelTypeList()
        {

            if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
            {
                oModelType = theModelType.GetModel(int.Parse(Request.QueryString["mid"]));
                if (oModelType != null)
                {
                    this.lblModelType.Text = oModelType.modelTypeName;
                }
                else
                {
                    this.lblModelType.Hidden = true;
                }
            }
            else
            {
                this.lblModelType.Hidden = true;
            }
        }


        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            oGoods = theGoods.GetModelByGoodsCode(this.lblGoodsCode.Text.Trim());
            if (oGoods != null)
            {
                oWarehouseGoods = new tbWarehouseGoods();

                oWarehouseGoods.creater = int.Parse(base.AdminUserId);
                oWarehouseGoods.createTime = DateTime.Now;
                oWarehouseGoods.goodsCount = int.Parse(this.txtCount.Text);
                oWarehouseGoods.goodsId = oGoods.goodsId;
                oWarehouseGoods.remark = this.txtRemark.Text.Trim();
                oWarehouseGoods.warehouseId = int.Parse(this.ddlWarehouse.SelectedItem.Value);
                oWarehouseGoods.GoodsBatchNo = this.lblGoodsBatchNo.Text.Trim();
                oWarehouseGoods.costPrice = decimal.Parse(this.lblCostPrice.Text);
                if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
                {
                    oWarehouseGoods.ModelTypeId = int.Parse(Request.QueryString["mid"]);
                }
                string sModelTypeId = "0";
                if (!string.IsNullOrEmpty(Request.QueryString["mid"]))
                {
                    sModelTypeId = Request.QueryString["mid"];
                }
                oStockOrderGoods = theStockOrderGoods.GetModel(Request.QueryString["code"], int.Parse(Request.QueryString["gid"]),int.Parse(sModelTypeId));
                if (oStockOrderGoods != null)
                {
                    if (oStockOrderGoods.goodsCount < oStockOrderGoods.InWarehouseCount + int.Parse(this.txtCount.Text))
                    {
                        ExtAspNet.Alert.Show(string.Format("入库数量不能大于{0}！", oStockOrderGoods.goodsCount - oStockOrderGoods.InWarehouseCount));
                        return;
                    }

                  
                    oStockOrderGoods.InWarehouseCount = oStockOrderGoods.InWarehouseCount + int.Parse(this.txtCount.Text);
                    theStockOrderGoods.Update(oStockOrderGoods);
                    theStockOrder.UpdateStockOrderStates(Request.QueryString["code"]);
                }
                if (theWarehouseGoods.Add(oWarehouseGoods) > 0)
                {
                    ExtAspNet.Alert.Show("添加成功！");
                    ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());

                }
            }
            else
            {
                ExtAspNet.Alert.Show("没有找到商品编号！");
            }
        }
    }
}